import { PokerCard, PokerType } from "./PokerCard.js";
import { ErrorPokerHand } from "./PokerHand/ErrorPokerHand.js";
import { SinglePokerHand } from "./PokerHand/SinglePokerHand.js";
import { PairPokerHand } from "./PokerHand/PairPokerHand.js";
import { RocketPokerHand } from "./PokerHand/RocketPokerHand.js";
import { ThirdCardPokerHand } from "./PokerHand/ThirdCardPokerHand.js";
import { BombPokerHand } from "./PokerHand/BombPokerHand.js";
import { ThirdWithSinglePokerHand } from "./PokerHand/ThirdWithSinglePokerHand.js";
import { ThirdWithPairPokerHand } from "./PokerHand/ThirdWithPairPokerHand.js";
import { StraightPokerHand } from "./PokerHand/StraightPokerHand.js";
import { StraightPairPokerHand } from "./PokerHand/StraightPairPokerHand.js";
import { StraightThirdPokerHand } from "./PokerHand/StraightThirdPokerHand.js";
import { PlaneWithSinglePokerHand } from "./PokerHand/PlaneWithSinglePokerHand.js";
import { PlaneWithPairPokerHand } from "./PokerHand/PlaneWithPairPokerHand.js";
import { FourWithPairPokerHand } from "./PokerHand/FourWithPairPokerHand.js";
import { FourWithSinglePokerHand } from "./PokerHand/FourWithSinglePokerHand.js";
import PokerHand from "./PokerHand/PokerHand.js";

/** 消息详情的格式 */
export interface MsgData {
  /** 消息类型 */
  type: string;
  /** 各种类型需要的数据 */
  data: any;
}

/**
 * 获取手牌的类型
 * @param pokerCards 需要判断的扑克牌集合
 */
export const getPokerCardsType = (pokerCards: PokerCard[]) => {
  const len = pokerCards.length;
  return new Promise<PokerHand>((resolve, reject) => {
    if (len === 1) {
      resolve(new SinglePokerHand(pokerCards));
      return;
    }

    if (len === 2) {
      if (RocketPokerHand.isStandard(pokerCards)) {
        resolve(new RocketPokerHand(pokerCards));
        return;
      }

      if (PairPokerHand.isStandard(pokerCards)) {
        resolve(new PairPokerHand(pokerCards));
        return;
      }
      resolve(new ErrorPokerHand(pokerCards));
      return;
    }

    if (len === 3) {
      resolve(
        ThirdCardPokerHand.isStandard(pokerCards)
          ? new ThirdCardPokerHand(pokerCards)
          : new ErrorPokerHand(pokerCards)
      );
      return;
    }

    if (len === 4) {
      if (BombPokerHand.isStandard(pokerCards)) {
        resolve(new BombPokerHand(pokerCards));
        return;
      }
      if (ThirdWithSinglePokerHand.isStandard(pokerCards)) {
        resolve(new ThirdWithSinglePokerHand(pokerCards));
        return;
      }
      resolve(new ErrorPokerHand(pokerCards));
      return;
    }

    if (ThirdWithPairPokerHand.isStandard(pokerCards)) {
      resolve(new ThirdWithPairPokerHand(pokerCards));
      return;
    }
    if (StraightPokerHand.isStandard(pokerCards)) {
      resolve(new StraightPokerHand(pokerCards));
      return;
    }

    if (StraightPairPokerHand.isStandard(pokerCards)) {
      resolve(new StraightPairPokerHand(pokerCards));
      return;
    }

    if (StraightThirdPokerHand.isStandard(pokerCards)) {
      resolve(new StraightThirdPokerHand(pokerCards));
      return;
    }

    if (PlaneWithSinglePokerHand.isStandard(pokerCards)) {
      if (FourWithPairPokerHand.isStandard(pokerCards)) {
        reject({
          code: 500,
          message: "符合两个",
          data: [PlaneWithPairPokerHand, FourWithPairPokerHand],
        });
        return;
      }
      resolve(new PlaneWithSinglePokerHand(pokerCards));
      return;
    }

    if (PlaneWithPairPokerHand.isStandard(pokerCards)) {
      resolve(new PlaneWithPairPokerHand(pokerCards));
      return;
    }

    if (FourWithSinglePokerHand.isStandard(pokerCards)) {
      resolve(new FourWithSinglePokerHand(pokerCards));
      return;
    }

    if (FourWithPairPokerHand.isStandard(pokerCards)) {
      resolve(new FourWithPairPokerHand(pokerCards));
      return;
    }
    resolve(new ErrorPokerHand(pokerCards));
    return;
  });
};
